﻿using CommonBase;
using InteractiveWhiteboardManagement.Base.Common;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.ServiceLocation;
using SurveillancePro.Base.Common;
using SurveillancePro.Base.ViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using ToolkitMgtModule.Views;

namespace ToolkitMgtModule.ViewModels
{
    /// <summary>
    /// 工具箱
    /// </summary>
    public class ViewModel_Toolkit : WorkspaceViewModel
    {
        #region Private Fields

        private DelegateCommand<string> _cmdViewToolkitItem;

        private readonly IRegionManager _regionManager;
        private readonly IServiceLocator _serviceLocator;

        #endregion

        #region Constructor

        public ViewModel_Toolkit(IRegionManager regionManager, IServiceLocator serviceLocator)
        {
            this._regionManager = regionManager;
            this._serviceLocator = serviceLocator;
        }

        #endregion

        #region Base Class Overrides

        protected override void OnDispose()
        {
        }

        #endregion

        #region Public Properties

        [Display(GroupName = "DelegateCommand")]
        public DelegateCommand<string> Cmd_ViewToolkitItem
        {
            get
            {
                if (_cmdViewToolkitItem == null)
                {
                    _cmdViewToolkitItem = new DelegateCommand<string>(_viewToolkitItem, _canViewToolkitItem);
                }
                return _cmdViewToolkitItem;
            }
        }

        #endregion

        #region Private Methods

        #region DelegateCommand

        private bool _canViewToolkitItem(string toolkitItem)
        {
            return true;
        }

        /// <summary>
        /// 工具箱功能
        /// </summary>
        /// <param name="toolkitItem">工具箱菜单</param>
        private void _viewToolkitItem(string toolkitItem)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(toolkitItem) || this._regionManager == null)
                    return;

                switch (toolkitItem)
                {
                    case "PlayBack":
                        object view = this._regionManager.Regions[RegionNames.ContentRegion].GetView(ViewNames.ToolkitMgtModule_PlayBack); // 根据视图名获取对应视图
                        if (view == null) // 若找不到对应视图则添加
                        {
                            View_PlayBack playBack = this._serviceLocator.GetInstance<Object>(ViewNames.ToolkitMgtModule_PlayBack) as View_PlayBack; // 解析远程录像回放界面
                            if (playBack != null && playBack.DataContext != null &&
                                playBack.DataContext is ViewModel_PlayBack)
                            {
                                ViewModel_PlayBack vm_PlayBack = playBack.DataContext as ViewModel_PlayBack;
                                vm_PlayBack.SearchItemList(); // 查询文件列表
                            }

                            this._regionManager.Regions[RegionNames.ContentRegion].Add(playBack, ViewNames.ToolkitMgtModule_PlayBack); // 添加新的视图到当前区域中 AddToRegion
                            view = playBack;
                        }
                        this._regionManager.Regions[RegionNames.ContentRegion].Activate(view); // 激活视图显示
                        break;
                    case "UserConfig":
                        this._regionManager.RequestNavigate(RegionNames.ContentRegion, 
                            ViewNames.ToolkitMgtModule_UserConfig); // 显示用户配置界面
                        break;


                    default:
                        break;
                }
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
                MessageBox.Show(ex.Message, ConstantString.MSG_COMPANY);
            }
        }

        #endregion

        #endregion
    }
}
